package com.medical.sys.service.impl;

import com.medical.common.paginator.domain.PageBounds;
import com.medical.common.paginator.domain.PageList;
import com.medical.sys.mapper.UserMapper;
import com.medical.sys.model.User;
import com.medical.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@SuppressWarnings("SpringJavaAutowiringInspection")
@Service("userService")
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PasswordHelper passwordHelper;

    public User selectByPrimaryKey(String id){
        return userMapper.selectByPrimaryKey(id);
    }

    public void saveUser(User user) {
        if(user != null){
            checkUsername(user);
            if(StringUtils.isEmpty(user.getId())){
                user.setId(UUID.randomUUID().toString());
            }
            user.setRegisterdate(new Date());
            passwordHelper.encryptPassword(user);
            userMapper.insert(user);
        }
    }

    private void checkUsername(User user){
        User exist = userMapper.selectByUsername(user.getUsername());
        if(exist != null && !exist.getId().equals(user.getId())){
            throw new CustomizeException("已存在相同用户名");
        }
    }
    public void updateUser(User user) {
        checkUsername(user);
        if(!StringUtils.isEmpty(user.getPassword())){
            passwordHelper.encryptPassword(user);
        }
        userMapper.updateByPrimaryKey(user);
    }

    public List<User> searchUsers(String query, Integer page, Integer pageSize, String sortColumn, String sortDir) {
        return searchUsers(query, "off", page, pageSize, sortColumn, sortDir);
    }

    public List<User> searchUsers(String query, String isaccount, Integer page, Integer pageSize, String sortColumn, String sortDir) {
        Map<String,Object> params = new HashMap<>();

        if(page==null){
            page=1;
        }
        if(pageSize==null){
            pageSize=10;
        }
        if(!StringUtils.isEmpty(sortColumn)){
            params.put("sortColumn", sortColumn);
        }else {
            params.put("sortColumn", "id");
        }
        if(!StringUtils.isEmpty(sortDir)){
            params.put("sortDir", sortDir);
        }else {
            params.put("sortDir", "asc");
        }
        if(StringUtils.isEmpty(isaccount)){
            isaccount = "off";
        }
        params.put("isaccount", isaccount);
        if(!StringUtils.isEmpty(query)){
            query = "%"+query+"%";
        }else {
            query = "%1%";
        }
        params.put("query", query);
        params.put("administrator", false);
        List<User> results = userMapper.selectByQuery(params, new PageBounds(page,pageSize));
        return (List<User>)results;
    }


    public PageList<User> findUsersByOrgId(String orgId, Integer page, Integer pageSize, String sortColumn, String sortDir) {
        Map<String,Object> params = new HashMap<>();

        if(page==null){
            page=1;
        }
        if(pageSize==null){
            pageSize=10;
        }
        if(!StringUtils.isEmpty(sortColumn)){
            params.put("sortColumn", sortColumn);
        }else {
            params.put("sortColumn", "id");
        }
        if(!StringUtils.isEmpty(sortDir)){
            params.put("sortDir", sortDir);
        }else {
            params.put("sortDir", "asc");
        }
        if(!StringUtils.isEmpty(orgId)){
            params.put("orgId", orgId);
        }else {
            throw new CustomizeException("orgId为空");
        }
        params.put("administrator", false);
        List<User> results = userMapper.findByOrgId(params, new PageBounds(page, pageSize));
        return (PageList<User>)results;
    }

    public void deleteUser(String userId) {
        deleteUser(userId, false);
    }

    public void deleteUser(String userId, Boolean administrator) {
        if(administrator){
            userMapper.deleteByPrimaryKey(userId);
        }else{
            User user = userMapper.selectByPrimaryKey(userId);
            if(user != null){
                user.setDeleted(true);
                userMapper.updateByPrimaryKey(user);
            }
        }
    }

    public List<User> getUsersByRoleId(String roleId) {
        if(!StringUtils.isEmpty(roleId)){
            roleId = "%"+roleId+"%";
        }
        return userMapper.selectByRoleId(roleId);
    }

    public List<User> getUsers(){
        return getUsers(false);
    }

    public List<User> getUsers(Boolean administrator){
        return userMapper.selectAll(administrator);
    }
    public List<User> selectByOrgId(String orgId){
        List<User> users = userMapper.selectByOrgId(orgId);
        return users;
    }
}
